import * as pinyin4js from 'pinyin4js';
export class Tools {
    static pinyin(word: string) {
        let r = pinyin4js.getShortPinyin(word)
        return r
    }

    static pinyinFull(word: string) {
        let r = pinyin4js.convertToPinyinString(word, '', pinyin4js.WITHOUT_TONE);
        return r;
    }

    static rounded(num: number) {
        return parseFloat(num.toFixed(2));
    }

    static pinyinTo9Num(py: string) {
        let map = {
            a: '2',
            b: '2',
            c: '2',
            d: '3',
            e: '3',
            f: '3',
            g: '4',
            h: '4',
            i: '4',
            j: '5',
            k: '5',
            l: '5',
            m: '6',
            n: '6',
            o: '6',
            p: '7',
            q: '7',
            r: '7',
            s: '7',
            t: '8',
            u: '8',
            v: '8',
            w: '9',
            x: '9',
            y: '9',
            z: '9'
        };

        let r = [];
        for (let c of py) {
            r.push(map[c]);
        }

        return r.join('');
    }

    static streamToBuffer(stream) {
        return new Promise((resolve, reject) => {
            let buffers = [];
            stream.on('error', reject);
            stream.on('data', (data) => buffers.push(data))
            stream.on('end', () => resolve(Buffer.concat(buffers)));
        });
    }

    static firstLetterCapital(str: string) {
        return str[0].toLocaleUpperCase() + str.substr(1, str.length - 1);
    }

    static getColor(type: string) {
        const list = [
            { color: '#4a4a4a', key: 'default' },
            { color: '#52c41a', key: 'wechat' },
            { color: '#1890ff', key: 'alipay' },
            { color: '#bf0a13', key: 'coupon' },
            { color: '#690abf', key: '' },
            { color: '#bf0a90', key: '' },
            { color: '#bf980a', key: '' },
            { color: '#0abf7c', key: '' },
            { color: '#bfb10a', key: '' },
            { color: '#0abf95', key: '' }
        ];

        if (type == 'random') {
            let min = 0;
            let max = list.length;
            let num = Math.floor(Math.random() * (max - min + 1) + min);

            return list[num];
        }
        else {
            return list.find(x => x.key == type);
        }
    }

    static checkMobile(phone) {
        if (!(/^1[3|4|5|8][0-9]\d{4,8}$/.test(phone))) {
            return false;
        }
        return true;
    }

    static checkOnlyNum(value: string) {
        return !(/[^0-9+$]/.test(value));
    }

    static checkOnlyChart(value: string) {
        return (/^[a-zA-Z]+$/.test(value));
    }

}